package graph.directedgraph.orthogonallist;

import graph.inter.CustGraph;

import java.util.Scanner;

/**
 * 有向图 -- 十字链表
 */
public class OrthogonalListGraph implements CustGraph {
    // 顶点数组
    private OrthVexNode[] vexs;
    // 顶点数
    private int numVertexes;
    // 边数
    private int numEdges;

    private Scanner sc;

    public OrthogonalListGraph() {
        sc = new Scanner(System.in);
    }

    @Override
    public void createGraph() {
        System.out.println("输入图的顶点数：");
        numVertexes = sc.nextInt();
        vexs = new OrthVexNode[numVertexes];
        System.out.println("输入图的边数");
        numEdges = sc.nextInt();
        System.out.println("输入图的顶点：");
        for (int i = 0; i < numVertexes; i++) {
            String s = sc.next();
            vexs[i] = new OrthVexNode(s);
        }
        for (int i = 0; i < numVertexes; i++) {
            System.out.print(i + "." + vexs[i].getData() + "     ");
        }
        System.out.println();
        for (int k = 0; k < numEdges; k++) {
            System.out.println("输入边（vi,vj）上的下标i,下标j:");
            int i = sc.nextInt();
            int j = sc.nextInt();
            OrthEdgeNode temp = new OrthEdgeNode(i, j, vexs[j].getFirstIn(), vexs[i].getFirstOut());
            if (!edgeExist(j, vexs[i])) {
                vexs[i].setFirstOut(temp);
                vexs[j].setFirstIn(temp);
            }
        }
    }

    @Override
    public int locateVex(String u) {
        return 0;
    }

    @Override
    public String getVex(int v) {
        return null;
    }

    @Override
    public void putVex(int v, String value) {

    }

    @Override
    public String firstAdjVex(int v) {
        return null;
    }

    @Override
    public String nextAdjVex(int v, int w) {
        return null;
    }

    @Override
    public void insertVex(String v) {

    }

    @Override
    public void deleteVex(String v) {

    }

    @Override
    public void insertArc(int v, int w) {

    }

    @Override
    public void deleteArc(int v, int w) {

    }

    @Override
    public void dfsTraverse(int v) {

    }

    @Override
    public void bfsTraverse(int v) {

    }

    public boolean edgeExist(int j, OrthVexNode vexNode) {
        OrthEdgeNode p = vexNode.getFirstOut();
        while (p != null) {
            if (p.getTailVex() == j) {
                return true;
            }
            p = p.getTailLink();
        }
        return false;
    }

    public OrthVexNode[] getVexs() {
        return vexs;
    }

    public void setVexs(OrthVexNode[] vexs) {
        this.vexs = vexs;
    }

    public int getNumVertexes() {
        return numVertexes;
    }

    public void setNumVertexes(int numVertexes) {
        this.numVertexes = numVertexes;
    }

    public int getNumEdges() {
        return numEdges;
    }

    public void setNumEdges(int numEdges) {
        this.numEdges = numEdges;
    }
}
